package com.cn.transfer.handle.impl.purchase.community;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.ProductCodeApplication;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 向伟典
 * @date: 2023/9/6 13:54
 * @Description: 公有的方法
 */
@Slf4j
public class PurchaseCommunity {


    /**
     * 根据code查询简道云数据
     */
    public static List<JdyPurchaseOrder> queryBased( Map<String, Object> parameter,Header[] headers,String voucherCode,String fieldName){
        Map<String ,Object> filter=new HashMap<>();
        filter.put("rel","and");
        Map<String ,Object> cond =new HashMap<>();
        cond.put("field",fieldName);
        cond.put("type","string");
        cond.put("method","eq");
        List<String> values =new ArrayList<>();
        values.add(voucherCode);
        cond.put("value",values);
        List<Map<String,Object>> conds=new ArrayList<>();
        conds.add(cond);
        filter.put("cond",conds);
        parameter.put("filter",filter);
        String jsonSale = JSONUtil.toJsonStr(parameter);
        JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
        String data = jdySale.getStr("data");
        JSONArray objects = JSONUtil.parseArray(data);
        return JSONUtil.toList(objects, JdyPurchaseOrder.class);
    }
    public static <T> List<T> queryBased( Map<String, Object> parameter,Header[] headers,String voucherCode,String fieldName,Class<T> clazz){
        Map<String ,Object> filter=new HashMap<>();
        filter.put("rel","and");
        Map<String ,Object> cond =new HashMap<>();
        cond.put("field",fieldName);
        cond.put("type","string");
        cond.put("method","eq");
        List<String> values =new ArrayList<>();
        values.add(voucherCode);
        cond.put("value",values);
        List<Map<String,Object>> conds=new ArrayList<>();
        conds.add(cond);
        filter.put("cond",conds);
        parameter.put("filter",filter);
        String jsonSale = JSONUtil.toJsonStr(parameter);
        JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
        String data = jdySale.getStr("data");
        JSONArray objects = JSONUtil.parseArray(data);
        return JSONUtil.toList(objects, clazz);
    }
    public static <T> List<T> queryBased( Map<String, Object> parameter,Header[] headers,HashMap<String,String> map,Class<T> clazz){
        Map<String ,Object> filter=new HashMap<>();
        filter.put("rel","and");
        List<Map<String,Object>> conds=new ArrayList<>();
        map.forEach((key,value) ->{
            Map<String ,Object> cond =new HashMap<>();
            cond.put("field",key);
            cond.put("type","string");
            cond.put("method","eq");
            List<String> values =new ArrayList<>();
            values.add(value);
            cond.put("value",values);
            conds.add(cond);
        });
        filter.put("cond",conds);
        parameter.put("filter",filter);
        String jsonSale = JSONUtil.toJsonStr(parameter);
        JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
        String data = jdySale.getStr("data");
        JSONArray objects = JSONUtil.parseArray(data);
        return JSONUtil.toList(objects, clazz);
    }
}
